import 'package:shared_preferences/shared_preferences.dart';

/// SharedPreferences工具类
/// 提供统一的本地存储操作接口
class SharedPreferencesTool {
  static SharedPreferences? _prefs;

  /// 初始化SharedPreferences
  static Future<void> init() async {
    _prefs ??= await SharedPreferences.getInstance();
  }

  /// 确保SharedPreferences已初始化
  static Future<SharedPreferences> _getPrefs() async {
    if (_prefs == null) {
      await init();
    }
    return _prefs!;
  }

  /// 保存字符串
  static Future<bool> setString(String key, String value) async {
    final prefs = await _getPrefs();
    return await prefs.setString(key, value);
  }

  /// 获取字符串
  static Future<String?> getString(String key) async {
    final prefs = await _getPrefs();
    return prefs.getString(key);
  }

  /// 保存整数
  static Future<bool> setInt(String key, int value) async {
    final prefs = await _getPrefs();
    return await prefs.setInt(key, value);
  }

  /// 获取整数
  static Future<int?> getInt(String key) async {
    final prefs = await _getPrefs();
    return prefs.getInt(key);
  }

  /// 保存布尔值
  static Future<bool> setBool(String key, bool value) async {
    final prefs = await _getPrefs();
    return await prefs.setBool(key, value);
  }

  /// 获取布尔值
  static Future<bool?> getBool(String key) async {
    final prefs = await _getPrefs();
    return prefs.getBool(key);
  }

  /// 保存双精度浮点数
  static Future<bool> setDouble(String key, double value) async {
    final prefs = await _getPrefs();
    return await prefs.setDouble(key, value);
  }

  /// 获取双精度浮点数
  static Future<double?> getDouble(String key) async {
    final prefs = await _getPrefs();
    return prefs.getDouble(key);
  }

  /// 保存字符串列表
  static Future<bool> setStringList(String key, List<String> value) async {
    final prefs = await _getPrefs();
    return await prefs.setStringList(key, value);
  }

  /// 获取字符串列表
  static Future<List<String>?> getStringList(String key) async {
    final prefs = await _getPrefs();
    return prefs.getStringList(key);
  }

  /// 删除指定键的数据
  static Future<bool> remove(String key) async {
    final prefs = await _getPrefs();
    return await prefs.remove(key);
  }

  /// 清除所有数据
  static Future<bool> clear() async {
    final prefs = await _getPrefs();
    return await prefs.clear();
  }

  /// 检查是否包含指定键
  static Future<bool> containsKey(String key) async {
    final prefs = await _getPrefs();
    return prefs.containsKey(key);
  }

  /// 获取所有键
  static Future<Set<String>> getKeys() async {
    final prefs = await _getPrefs();
    return prefs.getKeys();
  }

  /// 重新加载数据
  static Future<void> reload() async {
    final prefs = await _getPrefs();
    await prefs.reload();
  }

  // 常用的键名常量
  static const String keyUserToken = 'user_token';
  static const String keyUserInfo = 'user_info';
  static const String keyCurrentUsername = 'current_username';
  static const String keyCurrentUserId = 'current_user_id';
  static const String keySavedUsername = 'saved_username';
  static const String keySavedPassword = 'saved_password';
  static const String keyRememberPassword = 'remember_password';
  static const String keyIsLoggedIn = 'is_logged_in';
  static const String keyThemeMode = 'theme_mode';
  static const String keyLanguage = 'language';
  static const String keyFirstLaunch = 'first_launch';

  /// 保存用户登录信息
  static Future<void> saveLoginInfo({
    required String username,
    required String userId,
    required String token,
    String? password,
    bool rememberPassword = false,
  }) async {
    await setString(keyUserToken, token);
    await setString(keyCurrentUsername, username);
    await setString(keyCurrentUserId, userId);
    await setBool(keyIsLoggedIn, true);
    
    if (rememberPassword && password != null) {
      await setString(keySavedPassword, password);
      await setBool(keyRememberPassword, true);
    } else {
      await remove(keySavedPassword);
      await setBool(keyRememberPassword, false);
    }
  }

  /// 清除用户登录信息
  static Future<void> clearLoginInfo() async {
    await remove(keyUserToken);
    await remove(keyCurrentUsername);
    await remove(keyCurrentUserId);
    await remove(keySavedUsername);
    await remove(keySavedPassword);
    await setBool(keyIsLoggedIn, false);
    await setBool(keyRememberPassword, false);
  }

  /// 获取当前用户名
  static Future<String?> getCurrentUsername() async {
    return await getString(keyCurrentUsername);
  }

  /// 获取当前用户ID
  static Future<String?> getCurrentUserId() async {
    return await getString(keyCurrentUserId);
  }

  /// 检查是否已登录
  static Future<bool> isLoggedIn() async {
    return await getBool(keyIsLoggedIn) ?? false;
  }

  /// 检查是否记住密码
  static Future<bool> isRememberPassword() async {
    return await getBool(keyRememberPassword) ?? false;
  }

  /// 获取保存的密码
  static Future<String?> getSavedPassword() async {
    final remember = await isRememberPassword();
    if (remember) {
      return await getString(keySavedPassword);
    }
    return null;
  }
}